﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraScheduler;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;

namespace CRM_2013
{
    

    public partial class lessonsCtr : DevExpress.XtraEditors.XtraUserControl
    {

                
        public lessonsCtr()
        {
            InitializeComponent();
        }

        private void lessonsCtr_Load(object sender, EventArgs e)
        {
            loadDataLessons();
        }

        public void loadDataLessons()
        {
            lessonsTA.Fill(dataSet1.Lessons);
            productTA.Fill(dataSet1.Product);
            usersTA.Fill(dataSet1.Users);

            
        }

        public void updateDataLessons()
        {
            lessonsTA.Update(dataSet1.Lessons);
        }

        private void newLessonMenuClick(object sender, EventArgs e)
        {

            editTreeLessons(true);
            
        }

        private void childLessonMenu_Click(object sender, EventArgs e)
        {
            editTreeLessons(false);
        }

        private void editTreeLessons(bool editMain)
        {
            lessonsEditForm newForm = new lessonsEditForm();

            if (editMain)
            {
                newForm.isMain = true;
            }

            else
            {
                TreeListNode currNode = treeList1.FocusedNode;
                TreeListColumn idColumn = treeList1.Columns["idLessons"];
                TreeListColumn lessonColumn = treeList1.Columns["Lesson"];

                newForm.isMain = false;
                newForm.idParent = (Guid)currNode.GetValue(idColumn);
                newForm.nameParentLesson = currNode.GetValue(lessonColumn).ToString();
            }

            if (newForm.ShowDialog() == DialogResult.OK)
            {
                lessonsTA.Fill(dataSet1.Lessons);
                TreeListNode foundNode = treeList1.FindNodeByKeyID(newForm.idNew);
                foundNode.Selected = true;

            }


            newForm.Dispose();


        }

        private void removeMenu_Click(object sender, EventArgs e)
        {
            TreeListNode currNode = treeList1.FocusedNode;
            TreeListColumn idColumn = treeList1.Columns["idLessons"];
            TreeListColumn lessonColumn = treeList1.Columns["Lesson"];
            
            Guid id = (Guid)currNode.GetValue(idColumn);

            if (MessageBox.Show("ВНИМАНИЕ! Будут удалены все данные о тренинге '" + currNode.GetValue(lessonColumn).ToString()
                + "' его участниках и оплатах! Подтвердите удаление.", "Внимание! Запрос на необратимое действие!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                treeList1.Nodes.Remove(currNode);
            }
            
        }

        private void radioGroupFilterByActive_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (radioGroupFilterByActive.SelectedIndex)
            {
                case 0:
                    bsLessons.Filter = "IsPast=0";
                    break;
                case 1:
                    bsLessons.Filter = "IsPast=1";
                    break;
                case 2:
                    bsLessons.Filter = "";
                    break;
            }
        }
 

       



    }
}
